package com.chat.datea.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.chat.datea.R;
import com.chat.datea.bean.HotSingerEntity;
import com.chat.datea.utils.volley.VolleySingleton;

import java.util.List;

/**
 * Created by Shura on 15/10/27.
 */
public class SingerListAdapter extends BaseAdapter {

    private List<HotSingerEntity.ArtistEntity> mArtistEntities;
    private ImageLoader imageLoader;
    private VolleySingleton volleySingleton;
    private Context mContext;

    public SingerListAdapter(Context context, List<HotSingerEntity.ArtistEntity> artistEntities) {
        this.mContext = context;
        this.mArtistEntities = artistEntities;
        volleySingleton = VolleySingleton.getVolleySingleton(mContext);
        imageLoader = volleySingleton.getImageLoader();
    }

    @Override
    public int getCount() {
        return mArtistEntities != null && mArtistEntities.size() > 0 ? mArtistEntities.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return mArtistEntities != null && mArtistEntities.size() > 0 ? mArtistEntities.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            // 初始化缓存组件
            viewHolder = new ViewHolder();
            // 通过布局加载器加载布局
            convertView = LayoutInflater.from(mContext).inflate(R.layout.singer_listview_item, null);

            viewHolder.imageView = (ImageView) convertView.findViewById(R.id.singer_pic);
            viewHolder.textView = (TextView) convertView.findViewById(R.id.album_name);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.textView.setText(mArtistEntities.get(position).getName());

        String url = mArtistEntities.get(position).getAvatar_middle();
        ImageLoader.ImageListener imageListener = ImageLoader.getImageListener(viewHolder.imageView, R.mipmap.default_recommend, R.mipmap.default_recommend);
        imageLoader.get(url, imageListener);
        return convertView;
    }

    public class ViewHolder {
        private ImageView imageView;
        private TextView textView;
    }
}
